home *** CD-ROM | disk | FTP | other *** search
/ 17 Bit Software 6: Level 6 / 17 Bit - Level 6 (1998)(Epic Marketing)[!].iso / quartz / q0715.dms / q0715.adf / Devices / PrinterDevice / Example3.c < prev    next >
C/C++ Source or Header  |  1992-07-29  |  16KB  |  438 lines

  1. /***********************************************************/
  2. /*                                                         */
  3. /* Amiga C Encyclopedia (ACE) V3.0      Amiga C Club (ACC) */
  4. /* -------------------------------      ------------------ */
  5. /*                                                         */
  6. /* Book:    ACM Devices                 Amiga C Club       */
  7. /* Chapter: Printer Device              Tulevagen 22       */
  8. /* File:    Example3.c                  181 41  LIDINGO    */
  9. /* Author:  Anders Bjerin               SWEDEN             */
  10. /* Date:    92-04-27                                       */
  11. /* Version: 1.00                                           */
  12. /*                                                         */
  13. /*   Copyright 1992, Anders Bjerin - Amiga C Club (ACC)    */
  14. /*                                                         */
  15. /* Registered members may use this program freely in their */
  16. /*     own commercial/noncommercial programs/articles.     */
  17. /*                                                         */
  18. /***********************************************************/
  19.  
  20.  
  21.  
  22. /* This program demonstrates how you can send printer commands to */
  23. /* the Printer Device, which will translate these commands with   */
  24. /* help of Preferences, before they are sent to the printer.      */
  25.  
  26.  
  27.  
  28. #include <exec/types.h>       /* Data types.             */
  29. #include <exec/errors.h>      /* Exec error messages.    */
  30. #include <devices/printer.h>  /* Printer Device.         */
  31. #include <exec/io.h>          /* Standard request block. */
  32.  
  33.  
  34.  
  35. /* Declare how the printer request block look like: */
  36. union printerIO
  37. {
  38.   struct IOStdReq ios;
  39.   struct IODRPReq iodrp;
  40.   struct IOPrtCmdReq iopc;
  41. };
  42.  
  43. /* Declare a pointer to our reply port: */
  44. struct MsgPort *replymp = NULL;
  45.  
  46. /* Declare a pointer our printer request block: */
  47. union printerIO *printer_req = NULL;
  48.  
  49. /* Store the printer device error here: */
  50. UWORD printer_dever = TRUE;
  51.  
  52. /* Declare our data buffer: (12 characters + NULL sign) */
  53. BYTE buffer[] = "Amiga C Club";
  54.  
  55.  
  56.  
  57. /* Declare our functions: */
  58.  
  59. /* Our main function: */
  60. void main();
  61.  
  62. /* Clears and removes everything nice and neatly: */
  63. void clean_up( BYTE error, STRPTR text );
  64.  
  65. /* Prints some information about the error: */
  66. void PrtError( BYTE error );
  67.  
  68. /* Sends characters (which are translated) to the printer: */
  69. BYTE PrintText(
  70.   union printerIO *ioreq,
  71.   BYTE *data,
  72.   ULONG length
  73. );
  74.  
  75. /* Sends raw (untranslated) characters to the printer: */
  76. BYTE PrintRaw(
  77.   union printerIO *ioreq,
  78.   BYTE *data,
  79.   ULONG length
  80. );
  81.  
  82. /* Sends printer commands with up to four optional parameters: */
  83. BYTE PrinterCommand(
  84.   union printerIO *ioreq,
  85.   UWORD command,
  86.   UBYTE par0,
  87.   UBYTE par1,
  88.   UBYTE par2,
  89.   UBYTE par3
  90. );
  91.  
  92.  
  93.  
  94. void main()
  95. {
  96.   /* Error number: */
  97.   BYTE error;
  98.  
  99.  
  100.   
  101.   /* Get a reply port: (No name, priority 0) */
  102.   replymp = (struct MsgPort *)
  103.     CreatePort( NULL, 0 );
  104.   if( !replymp )
  105.     clean_up( 0, "Could not create the reply port!" );
  106.  
  107.  
  108.  
  109.   /* Create the printer request block: */
  110.   printer_req = (union printerIO *)
  111.     CreateExtIO( replymp, sizeof(union printerIO) );
  112.   if( !printer_req )
  113.     clean_up( 0, "Not enough memory for the printer request block!" );
  114.  
  115.  
  116.  
  117.   /* Open the Printer Device: */
  118.   printer_dever = OpenDevice( "printer.device", 0, printer_req, 0 );
  119.   if( printer_dever )
  120.     clean_up( 0, "Could not open the Printer Device!" );
  121.  
  122.  
  123.  
  124.   /* Set a line feed (LF) at the end of our buffer: */
  125.   buffer[12]=10; /* 10 = LF (ASCII) */
  126.  
  127.  
  128.  
  129.   /* Send a printer command: [Underline On] */
  130.   PrinterCommand
  131.   (
  132.     printer_req, /* Pointer to the printer request block. */
  133.     aSGR4,       /* The printer command: "underline On".  */
  134.     0,           /* Parameter 1, Not used.                */
  135.     0,           /* Parameter 2, Not used.                */
  136.     0,           /* Parameter 3, Not used.                */
  137.     0            /* Parameter 4, Not used.                */
  138.   );
  139.  
  140.   /* Send some text to the printer: (Will be translated) */
  141.   error = PrintText( printer_req, buffer, 13 );
  142.   if( error )
  143.     printf( "Problems while printing...\n" );
  144.  
  145.  
  146.   /* Send a printer command: [Italics On] */
  147.   PrinterCommand
  148.   (
  149.     printer_req, /* Pointer to the printer request block. */
  150.     aSGR3,       /* The printer command: "italics On".    */
  151.     0,           /* Parameter 1, Not used.                */
  152.     0,           /* Parameter 2, Not used.                */
  153.     0,           /* Parameter 3, Not used.                */
  154.     0            /* Parameter 4, Not used.                */
  155.   );
  156.  
  157.   /* Send some text to the printer: (Will be translated) */
  158.   error = PrintText( printer_req, buffer, 13 );
  159.   if( error )
  160.     printf( "Problems while printing...\n" );
  161.  
  162.  
  163.   /* Send a printer command: [Underline Off] */
  164.   PrinterCommand
  165.   (
  166.     printer_req, /* Pointer to the printer request block. */
  167.     aSGR24,      /* The printer command: "underline Off". */
  168.     0,           /* Parameter 1, Not used.                */
  169.     0,           /* Parameter 2, Not used.                */
  170.     0,           /* Parameter 3, Not used.                */
  171.     0            /* Parameter 4, Not used.                */
  172.   );
  173.  
  174.  
  175.   /* Send a printer command: [Italics Off] */
  176.   PrinterCommand
  177.   (
  178.     printer_req, /* Pointer to the printer request block. */
  179.     aSGR23,      /* The printer command: "italics Off".   */
  180.     0,           /* Parameter 1, Not used.                */
  181.     0,           /* Parameter 2, Not used.                */
  182.     0,           /* Parameter 3, Not used.                */
  183.     0            /* Parameter 4, Not used.                */
  184.   );
  185.  
  186.  
  187.   /* Send a printer command: */
  188.   /* [Left margin:  30] */
  189.   /* [Right margin: 70] */
  190.   PrinterCommand
  191.   (
  192.     printer_req, /* Pointer to the printer request block.         */
  193.     aSLRM,       /* The printer command: "Set Left Right Margins. */
  194.     30,          /* Parameter 1, Set left margin to 30.           */
  195.     70,          /* Parameter 2, Set right margin to 70.          */
  196.     0,           /* Parameter 3, Not used.                        */
  197.     0            /* Parameter 4, Not used.                        */
  198.   );
  199.  
  200.   /* Send some text to the printer: (Will be translated) */
  201.   error = PrintText( printer_req, buffer, 12 );
  202.   if( error )
  203.     printf( "Problems while printing...\n" );
  204.  
  205.  
  206.  
  207.   /* Clean up and quit: */
  208.   clean_up( 0, "The End!" );
  209. }
  210.  
  211.  
  212.  
  213. /* Close and return everything that has been */
  214. /* opened and allocated before we quit:      */
  215.  
  216. void clean_up( BYTE error, STRPTR text )
  217. {
  218.   /* Print some information about the problem: */
  219.   if( error )
  220.     PrtError( error );
  221.  
  222.   /* Close the Printer Device: */ 
  223.   if( !printer_dever )
  224.     CloseDevice( printer_req );
  225.  
  226.   /* Deallocate the printer request block: */
  227.   if( printer_req )
  228.     DeleteExtIO( printer_req, sizeof(union printerIO) );
  229.  
  230.   /* Remove the replyport: */
  231.   if( replymp )
  232.     DeletePort( replymp);
  233.  
  234.   /* Print the message: */
  235.   printf( "\n%s\n", text );
  236.  
  237.   /* Quit: */
  238.   exit( 0 );
  239. }
  240.  
  241.  
  242. /* PrtError() tells the user what went wrong. You give it the error code */
  243. /* you received, and PrtError() will print a short description of the    */
  244. /* problem. Useful when debugging. (Printer errors)                      */
  245. /*                                                                       */
  246. /* Synopsis: PrtError( error );                                          */
  247. /*                                                                       */
  248. /* error:    (BYTE) The error value you want to have explained.          */
  249.  
  250. void PrtError( BYTE error )
  251. {
  252.   switch( error )
  253.   {
  254.     /* EXEC error messages: (defined in "exec/errors.h") */
  255.     case IOERR_OPENFAIL:
  256.       printf( "Could not open the device!\n" );
  257.       break;
  258.  
  259.     case IOERR_ABORTED:
  260.       printf( "The request was aborted!\n" );
  261.       break;
  262.  
  263.     case IOERR_NOCMD:
  264.       printf( "Unknown Command!\n" );
  265.       break;
  266.  
  267.     case IOERR_BADLENGTH:
  268.       printf( "Bad length of the command - data!\n" );
  269.  
  270.  
  271.     /* Printer Device errors: (defined in "devices/printer.h") */
  272.     case PDERR_CANCEL:
  273.       printf( "User cancelled the request!\n" );
  274.       break;
  275.       
  276.     case PDERR_NOTGRAPHICS:
  277.       printf( "The printer does not support graphics!\n" );
  278.       break;
  279.  
  280.     case PDERR_BADDIMENSION:
  281.       printf( "The printer dimension is not valid!\n" );
  282.       break;
  283.       
  284.  
  285.     case PDERR_INTERNALMEMORY:
  286.       printf( "Not enough memory for the internal printer functions!\n" );
  287.       break;
  288.  
  289.     case PDERR_BUFFERMEMORY:
  290.       printf( "Not enough memory for the print buffer!\n" );
  291.       break;
  292.  
  293.     default:
  294.       printf( "An unknown error was reported! Error nr: %d\n", error );
  295.   }
  296. }
  297.  
  298.  
  299. /* PrintText() sends characters (which will be translated) to the */
  300. /* printer. Since the printer device will use the Preference's    */
  301. /* settings, it will know to which port (parallel or serial) the  */
  302. /* printer is connected to, what type of printer it is, and what  */
  303. /* special settings (margins, density, quality mode etc) the user */
  304. /* have defined.                                                  */
  305. /*                                                                */
  306. /* Note! All characters which are sent with this function may be  */
  307. /* translated by Preferences before it is passed on to the        */
  308. /* printer.                                                       */
  309. /*                                                                */
  310. /* Synopsis: error = PrintText( io, data, length );               */
  311. /*                                                                */
  312. /* error:    (BYTE) PrintWrite() returns 0 if everything was OK,  */
  313. /*           else an error number is returned.                    */
  314. /*                                                                */
  315. /* io:       (union printerIO *) Pointer to a printer request     */
  316. /*           block.                                               */
  317. /*                                                                */
  318. /* data:     (BYTE *) Pointer to the first character that should  */
  319. /*           be printed.                                          */
  320. /*                                                                */
  321. /* length    (ULONG) How many characters (bytes) you want to send */
  322. /*           to the printer.                                      */
  323.  
  324. BYTE PrintText(
  325.   union printerIO *ioreq, /* Pointer to the printer request block.          */
  326.   BYTE *data,             /* Pointer to the data which should be printed.   */
  327.   ULONG length            /* How many characters (bytes) should be printed. */
  328. )
  329. {
  330.   /* We want to print some text: (send data to PRT:) */
  331.   ioreq->ios.io_Command = CMD_WRITE;
  332.  
  333.   /* Give the start address of our data: */
  334.   ioreq->ios.io_Data = (APTR) data;
  335.  
  336.   /* Set number of chracters that should be printed: */
  337.   ioreq->ios.io_Length = length;
  338.  
  339.   /* Do our request, and return 0 if everything is OK, else */
  340.   /* return an error number: (This is a task sleep.)        */
  341.   return( (BYTE) DoIO( ioreq ) );
  342. }
  343.  
  344.  
  345.  
  346. /* PrintRaw() sends untranslated characters to the printer. Note   */
  347. /* that this is usually not a very good idea. Since the characters */
  348. /* will not be translated by Preferences, you can not be sure that */
  349. /* the characters you send will be the same when printed.          */ 
  350. /*                                                                 */
  351. /* Synopsis: error = PrintRaw( io, data, length );                 */
  352. /*                                                                 */
  353. /* error:    (BYTE) PrintRaw() returns 0 if everything was OK,     */
  354. /*           else an error number is returned.                     */
  355. /*                                                                 */
  356. /* io:       (union printerIO *) Pointer to a printer request      */
  357. /*           block.                                                */
  358. /*                                                                 */
  359. /* data:     (BYTE *) Pointer to the first character that should   */
  360. /*           be printed.                                           */
  361. /*                                                                 */
  362. /* length    (ULONG) How many characters (bytes) you want to send  */
  363. /*           to the printer.                                       */
  364.  
  365. BYTE PrintRaw(
  366.   union printerIO *ioreq, /* Pointer to the printer request block.          */
  367.   BYTE *data,             /* Pointer to the data which should be printed.   */
  368.   ULONG length            /* How many characters (bytes) should be printed. */
  369. )
  370. {
  371.   /* We want to print some raw (untranslated) text: */
  372.   ioreq->ios.io_Command = PRD_RAWWRITE;
  373.  
  374.   /* Give the start address of our data: */
  375.   ioreq->ios.io_Data = (APTR) data;
  376.  
  377.   /* Set number of chracters that should be printed: */
  378.   ioreq->ios.io_Length = length;
  379.  
  380.   /* Do our request, and return 0 if everything is OK, else */
  381.   /* return an error number: (This is a task sleep.)        */
  382.   return( (BYTE) DoIO( ioreq ) );
  383. }
  384.  
  385.  
  386.  
  387. /* PrinterCommand() sends a printer command with up to four optional         */
  388. /* arguments. See this chapter docummentation for more information about     */
  389. /* printer commands.                                                         */
  390. /*                                                                           */
  391. /* Synopsis: error = PrinterCommand( io, command, par1, par2, par3, par 4 ); */
  392. /*                                                                           */
  393. /* error:    (BYTE) If the command could not be sent to the printer an       */
  394. /*           error number is returned. If everything was OK, NULL is         */
  395. /*           returned.                                                       */
  396. /*                                                                           */
  397. /* io:       (union printerIO *) Pointer to a printer request block.         */
  398. /*                                                                           */
  399. /* command:  (UWORD) The command that should be sent to the printer. If the  */
  400. /*           command should be followed by one or more parameters, set these */
  401. /*           parameters in the following fields:                             */
  402. /*                                                                           */
  403. /* par1:     (UBYTE) The first parameter.                                    */
  404. /*                                                                           */
  405. /* par2:     (UBYTE) The second parameter.                                   */
  406. /*                                                                           */
  407. /* par3:     (UBYTE) The third parameter.                                    */
  408. /*                                                                           */
  409. /* par4:     (UBYTE) The fourth parameter.                                   */
  410.  
  411. BYTE PrinterCommand(
  412.   union printerIO *ioreq, /* Pointer to the printer request block. */
  413.   UWORD command,          /* The printer command.                  */
  414.   UBYTE par0,             /* The first parameter.                  */
  415.   UBYTE par1,             /* The second parameter.                 */
  416.   UBYTE par2,             /* The third parameter.                  */
  417.   UBYTE par3              /* The fourth parameter.                  */
  418. )
  419. {
  420.   /* We want to send a printer command to the printer: */
  421.   ioreq->iopc.io_Command = PRD_PRTCOMMAND;
  422.  
  423.   /* Set the printer command: */
  424.   ioreq->iopc.io_PrtCommand = command;
  425.  
  426.   /* Set the parameters: */
  427.   ioreq->iopc.io_Parm0 = par0;
  428.   ioreq->iopc.io_Parm1 = par1;
  429.   ioreq->iopc.io_Parm2 = par2;
  430.   ioreq->iopc.io_Parm3 = par3;
  431.  
  432.   /* Do our request, and return 0 if everything is OK, else */
  433.   /* return an error number: (This is a task sleep.)        */
  434.   return( (BYTE) DoIO( ioreq ) );
  435. }
  436.  
  437.  
  438.